package com.example.demo.controller;

import com.example.demo.modbus.ModbusUtils;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("modbus")
public class ModbusController {
    @Autowired
    ModbusUtils modbusUtils;

    /**
     * modbus读取只读寄存器
     * @param ip
     * @param port
     * @param start
     * @param length
     * @return
     *
     * testUrl: http://localhost:8080/modbus/modbusReadHoldingRegister?ip=127.0.0.1&port=502&start=102&length=1
     */
    @RequestMapping("readHoldingRegister")
    public short[] readHoldingRegister(String ip,int port,int start,int length){
        short[] ret = new short[0];
        try {
            ret = modbusUtils.readHoldingRegister(ip,port,start,length);
        } catch (ModbusTransportException e) {
            e.printStackTrace();
        }
        return ret;
    }


}
